package com.xhtt.modules.app.controller.sys;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xhtt.common.exception.RRException;
import com.xhtt.common.utils.Constant;
import com.xhtt.common.utils.PageUtils;
import com.xhtt.common.utils.R;
import com.xhtt.common.validator.ValidatorUtils;
import com.xhtt.modules.app.annotation.Login;
import com.xhtt.modules.sys.controller.AbstractController;
import com.xhtt.modules.sys.entity.SysDeptEntity;
import com.xhtt.modules.sys.entity.SysPositionEntity;
import com.xhtt.modules.sys.entity.SysRoleEntity;
import com.xhtt.modules.sys.service.SysDeptService;
import com.xhtt.modules.sys.service.SysPositionService;
import com.xhtt.modules.sys.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 职位管理
 */
@Api(value = "系统管理-职位管理", tags = "系统管理-职位管理")
@RestController
@RequestMapping("app/sys/position")
public class SysPositionController extends AbstractController {
    @Autowired
    private SysPositionService sysPositionService;
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 职位列表
     */
    @ApiOperation("职位列表(web/app)")
    @GetMapping("/list")
    @Login
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = sysPositionService.queryPositionPage(params);
        return R.ok().put(Constant.DATA, page);
    }

    /**
     * 新增职位
     */
    @ApiOperation("新增职位(web)")
    @PostMapping("/save")
    @Login
    public R save(@RequestBody SysPositionEntity position) {
        ValidatorUtils.validateEntity(position);
        int count = sysPositionService.count(new LambdaQueryWrapper<SysPositionEntity>().eq(SysPositionEntity::getName, position.getName()));
        if (count > 0) {
            throw new RRException("对象已存在");
        }
        sysPositionService.save(position);
        return R.ok();
    }

    /**
     * 获取详情
     */
    @ApiOperation("获取详情(web/app)")
    @GetMapping("/info/{positionId}")
    @Login
    public R info(@PathVariable("positionId") Integer positionId) {
        SysPositionEntity position = sysPositionService.getById(positionId);
        Integer deptId = position.getDeptId();
        Integer roleId = position.getRoleId();
        if (null != deptId) {
            SysDeptEntity dept = sysDeptService.getOne(new QueryWrapper<SysDeptEntity>().eq(SysDeptEntity.DEPTID, deptId).select(SysDeptEntity.DEPTID, SysDeptEntity.NAME));
            if (null != dept) {
                position.setDeptName(dept.getName());
            }
        }
        if (null != roleId) {
            SysRoleEntity role = sysRoleService.getOne(new QueryWrapper<SysRoleEntity>().eq(SysRoleEntity.ROLEID, roleId).select(SysRoleEntity.ROLEID, SysRoleEntity.NAME));
            if (null != role) {
                position.setRoleName(role.getName());
            }
        }

        return R.ok().put(Constant.DATA, position);
    }

    /**
     * 修改职位
     */
    @ApiOperation("修改职位(web)")
    @PostMapping("/update")
    @Login
    public R update(@RequestBody SysPositionEntity position) {
        ValidatorUtils.validateEntity(position);
        if (null == position.getPositionId() || position.getPositionId() < 1) {
            throw new RRException("参数有误");
        }
        int count = sysPositionService.count(new LambdaQueryWrapper<SysPositionEntity>().eq(SysPositionEntity::getName, position.getName()).ne(SysPositionEntity::getPositionId, position.getPositionId()));
        if (count > 0) {
            throw new RRException("对象已存在");
        }
        sysPositionService.updateById(position);
        return R.ok();
    }

    /**
     * 删除职位
     */
    @ApiOperation("删除职位(web)")
    @PostMapping("/delete")
    @Login
    public R delete(@RequestBody Integer[] positionIds) {
        sysPositionService.deletePosition(positionIds[0]);
        return R.ok();
    }

}
